const { Pool } = require('pg');

const pool = new Pool({
  host: 'localhost',
  port: 5432,
  database: 'dw_sql_manager',
  user: 'postgres',
  password: '1234Qwer'
});

async function checkScriptsTable() {
  try {
    console.log('检查scripts表结构...');
    
    // 检查表结构
    const tableStructure = await pool.query(`
      SELECT column_name, data_type, is_nullable, column_default
      FROM information_schema.columns 
      WHERE table_name = 'scripts' 
      ORDER BY ordinal_position
    `);
    
    console.log('\nscripts表字段:');
    tableStructure.rows.forEach(row => {
      console.log(`- ${row.column_name}: ${row.data_type} (nullable: ${row.is_nullable})`);
    });
    
    // 检查tags字段的具体类型
    const tagsField = await pool.query(`
      SELECT column_name, data_type, udt_name
      FROM information_schema.columns 
      WHERE table_name = 'scripts' AND column_name = 'tags'
    `);
    
    if (tagsField.rows.length > 0) {
      console.log('\ntags字段详细信息:');
      console.log(tagsField.rows[0]);
    }
    
    // 检查是否有数据
    const dataCount = await pool.query('SELECT COUNT(*) FROM scripts');
    console.log(`\nscripts表记录数: ${dataCount.rows[0].count}`);
    
    // 如果有数据，查看tags字段的示例
    if (parseInt(dataCount.rows[0].count) > 0) {
      const sampleData = await pool.query('SELECT id, name, tags FROM scripts LIMIT 3');
      console.log('\n示例数据:');
      sampleData.rows.forEach(row => {
        console.log(`- ${row.name}: tags = ${JSON.stringify(row.tags)} (type: ${typeof row.tags})`);
      });
    }
    
  } catch (error) {
    console.error('检查失败:', error.message);
  } finally {
    await pool.end();
  }
}

checkScriptsTable();